var query = {
	basePath: '',
	init: function(basePath) {
		this.basePath = basePath;
		this.initEvent();
		this.showResultTable();
		this.showCriteria();
	},

	initEvent: function() {
		$('#selection_dataType').change(function(option) {
			var queryLevel = $('#selection_dataType').find('option:selected').attr('queryLevel');
			query.showCriteria();
			query.showResultTable();
			query.searchWithoutDate(queryLevel);
			$('#start_dt').val('');
			$('#end_dt').val('');
		});
		
		$('#btn_search').click(function() {
			query.search();
		});
	},
	
	showResultTable: function() {
		var $tables = $('#result').children('table');
		for (var i = 0, l = $tables.length; i < l; i++) {
			var $resultTable = $('#' + $tables[i].id);
			$resultTable.hide();
			$resultTable.addClass('mytable');
		}
		// show the selected data type's result table
		var tableId = 't_' + $('#selection_dataType').find('option:selected').val();
		$('#' + tableId).show();
	},
	
	showCriteria: function() {
		var queryLevel = $('#selection_dataType').find('option:selected').attr('queryLevel');
		switch (queryLevel) {
			case '0':
				$('#search_timeType').hide();
				$('#search_baseCriteria').hide();
				$('#search_message').hide();
				break;
			case '1':
				$('#search_timeType').hide();
				$('#search_baseCriteria').show();
				$('#search_message').hide();
				break;
			case '2':
				$('#search_timeType').show();
				$('#search_baseCriteria').show();
				$('#search_message').show();
				break;
			default:
				break;
		}
	},
	
	search: function() {
		var $selectedOpt = $('#selection_dataType').find('option:selected'),
			queryLevel = $selectedOpt.attr('queryLevel'),
			datasource = $selectedOpt.val(),
			criteria = {};
		criteria.datasource = datasource;
		if (queryLevel == '1' || queryLevel == '2') {
			criteria.start_dt = $('#start_dt').val();
			criteria.end_dt = $('#end_dt').val();
			if ($.isEmptyObject(criteria.start_dt) || $.isEmptyObject(criteria.end_dt)) {
				alert('请输入完整的时间区间，以便查询');
				return;
			}
		}
		//alert(criteria.datasource + ' ' + criteria.start_dt + ' ' + criteria.end_dt);
		//把search条件传回后台，有些会有时间的search，有些没有
		this.getSearchResult(criteria);
	},
	
	searchWithoutDate: function(queryLevel) {
		if (queryLevel == '0') {
			this.search();
		}
	},
	
	getSearchResult: function(criteria) {
		// Ajax call to get the criteria data and load into related table
		if ('quantum' == criteria.datasource) {
			this.quantumAjax(criteria);
		} else if ('stores' == criteria.datasource) {
			this.storesAjax(criteria);
		} else if ('products' == criteria.datasource) {
			this.productsAjax(criteria);
		}
	},
	
	quantumAjax: function(criteria) {
		$.ajax({
			url: this.basePath + 'data/item_data!list.action?beginTime='+criteria.start_dt+'&endTime='+criteria.end_dt
		})
		.done(function(response) {
			$('#t_quantum').find('tr[name="dataTr"]').remove();
			var data = JSON.parse(response),
				htmlStr = '';
			if (data.length == 0) {
				alert('没有查到对应的结果，请从新查询。');
				return;
			}
			for (var i = 0, l = data.length; i < l; i++) {
				htmlStr += '<tr name="dataTr">';
				htmlStr += '<td>' + data[i].itemDataKey['no'] + '</td>';
				htmlStr += '<td>' + data[i].store_name + '</td>';
				htmlStr += '<td>' + data[i].itemDataKey['data_date'] + '</td>';
				htmlStr += '<td>' + data[i].views + '</td>';
				htmlStr += '<td>' + data[i].guests + '</td>';
				htmlStr += '<td>' + data[i].avg_item + '</td>';
				htmlStr += '<td>' + data[i].avg_cost + '</td>';
				htmlStr += '<td>' + data[i].success_rate + '</td>';
				htmlStr += '<td>' + data[i].networkio + '</td>';
				htmlStr += '</tr>';
			}
			$('#t_quantum').append(htmlStr);
		});
	},
	
	storesAjax: function(criteria) {
		$.ajax({
			url: this.basePath + 'data/stores!list.action?beginTime='+criteria.start_dt+'&endTime='+criteria.end_dt
		})
		.done(function(response) {
			$('#t_stores').find('tr[name="dataTr"]').remove();
			var data = JSON.parse(response),
				htmlStr = '';
			if (data.length == 0) {
				alert('没有查到对应的结果，请从新查询。');
				return;
			}
			for (var i = 0, l = data.length; i < l; i++) {
				htmlStr += '<tr name="dataTr">';
				htmlStr += '<td>' + data[i].no + '</td>';
				htmlStr += '<td>' + data[i].name + '</td>';
				htmlStr += '<td>' + data[i].attribute_id + '</td>';
				htmlStr += '<td>' + data[i].attribute + '</td>';
				htmlStr += '<td>' + data[i].updatedt + '</td>';
				htmlStr += '<td>' + data[i].updateby + '</td>';
				htmlStr += '</tr>';
			}
			$('#t_stores').append(htmlStr);
		});
	},
	
	productsAjax: function(criteria) {
		$.ajax({
			url: this.basePath + 'data/products!list.action?beginTime='+criteria.start_dt+'&endTime='+criteria.end_dt
		})
		.done(function(response) {
			$('#t_products').find('tr[name="dataTr"]').remove();
			var data = JSON.parse(response),
				htmlStr = '';
			if (data.length == 0) {
				alert('没有查到对应的结果，请从新查询。');
				return;
			}
			for (var i = 0, l = data.length; i < l; i++) {
				htmlStr += '<tr name="dataTr">';
				htmlStr += '<td>' + data[i].attribute + '</td>';
				htmlStr += '<td>' + data[i].providenote + '</td>';
				htmlStr += '<td>' + data[i].code + '</td>';
				htmlStr += '<td>' + data[i].productsKey.product_id + '</td>';
				htmlStr += '<td>' + data[i].productsKey.sku + '</td>';
				htmlStr += '<td>' + data[i].updatedt + '</td>';
				htmlStr += '<td>' + data[i].updateby + '</td>';
				htmlStr += '</tr>';
			}
			$('#t_products').append(htmlStr);
		});
	}
};